<!doctype html>
<html>
<head>
    <title>agnes Tests</title>
    <script type="text/javascript" src="agnes.js"></script>
    <link rel="stylesheet" type="text/css" href="agnes.css" />
    <script type='text/javascript'>
        function $(id) {
            return document.getElementById(id);
        }
        var rowDelimiter;
        function go(example) {
            rowDelimiter = agnes.rowDelimiter();
            testResult = tryAll(testsJsonToCsv);
        }
        function jalert(o) {
            alert(JSON.stringify(o));
        }
        function go1(num) {
            rowDelimiter = agnes.rowDelimiter();
            resultAll = tryOne(testsJsonToCsv[num], true);
            indicate(resultAll);
        }

        function indicate(resultAll) {
            if (resultAll) {
                sayResult("ALL TESTS PASSED");
                $('error').style.border = "5px solid #0A0";
            } else {
                sayResult("NOT ALL TESTS PASSED");
                $('error').style.border = "5px solid #F00";
            }
        }

        var testsJsonToCsv = [
			{ 'note': 'one field, one row',
			    'data': 'Name\r\nJack',
			    'result': '[{"Name":"Jack"}]'
			},
			{ 'note': 'two fields, one row',
			    'data': 'Name,Age\r\nJack,30',
			    'result': '[{"Name":"Jack","Age":"30"}]'
			},
			{ 'note': 'two fields, two rows',
			    'data': 'Name,Age\r\nJack,30\r\nJill,50',
			    'result': '[{"Name":"Jack","Age":"30"},{"Name":"Jill","Age":"50"}]'
			},
			{ 'note': 'trailing empty row',
			    'data': 'name,age\r\nfred,12\r\n',
			    'result': '[{"name":"fred","age":"12"}]'
			},
			{ 'note': 'empty row in the middle',
			    'data': 'name,age\r\n\r\nfred,12',
			    'result': '[{"name":"fred","age":"12"}]'
			},
			{ 'note': 'unquoted space is null',
			    'data': 'name,age\r\nfred,',
			    'result': '[{"name":"fred","age":null}]'
			},
			{ 'note': 'empty quotes is not null',
			    'data': 'name,age\r\nfred,""',
			    'result': '[{"name":"fred","age":""}]'
			},
			{ 'note': 'qualifiers contain a literal delim',
			    'data': 'name,age\r\nfred,","',
			    'result': '[{"name":"fred","age":","}]'
			},
			{ 'data': 'fred,jack\r\njim,bob',
			    'note': 'basic test',
			    'result': '[{"fred":"jim","jack":"bob"}]'
			},
		 { 'data': 'fred ,jack\r\njim,bob',
		     'note': 'trailing space',
		     'result': '[{"fred":"jim","jack":"bob"}]'
		 },
		 { 'data': 'fred,jack\r\n,bob',
		     'note': 'empty field is null',
		     'result': '[{"fred":null,"jack":"bob"}]'
		 },
		 { 'data': 'a,b\r\njim,',
		     'note': 'empty field at end of row is null',
		     'result': '[{"a":"jim","b":null}]'
		 },
		 { 'data': 'jim,bob\r\n ,jack',
		     'note': 'empty field with white space is null',
		     'result': '[{"jim":null,"bob":"jack"}]'
		 },
		 { 'data': 'fred,jack \r\njim,bob',
		     'note': 'trailing space at end of line is ignored',
		     'result': '[{"fred":"jim","jack":"bob"}]'
		 },
		 { 'data': 'fred,"jack"\r\njim,bob',
		     'note': 'surrounding double-quotes are stripped out',
		     'result': '[{"fred":"jim","jack":"bob"}]'
		 },
		 { 'data': 'a, "jack12"\r\njim,bob',
		     'note': 'space before a surrounding qualifier is ignored',
		     'result': '[{"a":"jim","jack12":"bob"}]'
		 },
		 { 'data': 'a,"jac,k"\r\njim,bob',
		     'note': 'field delimiters are ignored inside qualified fields',
		     'result': '[{"a":"jim","jac,k":"bob"}]'
		 },
				{ 'data': 'a,"jac""k"\r\njim,bob',
				    'note': 'double-quotes are un-doubled inside qualified fields',
				    'result': '[{"a":"jim","jac\\\"k":"bob"}]'
				},
		 { 'data': '" ",jack\r\njim,bob',
		     'note': 'qualified space is not null',
		     'result': '[{" ":"jim","jack":"bob"}]'
		 },
		 { 'data': '"  ",jack\r\njim,bob',
		     'note': 'qualified space is not reduced',
		     'result': '[{"  ":"jim","jack":"bob"}]'
		 },
		 { 'data': 'a,"jack "\r\njim,bob',
		     'note': 'qualified trailing space is not ignored',
		     'result': '[{"a":"jim","jack ":"bob"}]'
		 },
		 { 'data': 'a,"jack   "\r\njim,bob',
		     'note': 'qualified trailing space is not reduced',
		     'result': '[{"a":"jim","jack   ":"bob"}]'
		 },
		{ 'data': '"Title","Start Time", "Start Date"\r\n"Sample One", "09:30 AM", "2012-09-09"',
		     'note': 'example where everything is qualified',
		     'result': '[{"Title":"Sample One","Start Time":"09:30 AM","Start Date":"2012-09-09"}]'
		 },		 
		{ 'data': '"Title", "Start Time", "Start Date", "Notes"\r\n"Sample One", "09:30 AM", "2012-09-09", "Will do.\r\nLook, line feeds in this last field.\r\nYup."',
		     'note': 'everything is qualified and row delims in final row',
		     'result': '[{"Title":"Sample One","Start Time":"09:30 AM","Start Date":"2012-09-09","Notes":"Will do.\\r\\nLook, line feeds in this last field.\\r\\nYup."}]'
		 },		 
		];
        function test(tests) {
            var testResult = tryAll(tests);
            if (testResult) {
                sayResult("ALL TESTS PASSED");
                $('error').style.border = "5px solid #0A0";
                return;
            } else {
                sayResult("NOT ALL TESTS PASSED");
                $('error').style.border = "5px solid #F00";
            }
        }
        function tryOne(test, verbose) {
            var result;
            result = JSON.stringify(agnes.csvToJson(test.data));
            if (test.result === result) {
                sayResult('PASSED: ' + test.note);
				if (verbose === true) {
				  sayResult('input:\r\n' + test.data);
                  sayResult("Expected \r\n'" + test.result + "'\r\nand found: \r\n'" + result + "'");
				}
                return true;
            } else {
                sayResult('*** FAILED: ' + test.note + ' *** ');
                //sayResult('Failed.');
                sayResult('input:\r\n' + test.data);
                sayResult("Expected \r\n'" + test.result + "'\r\nbut found: \r\n'" + result + "'");
            }
        }
        function tryAll(tests) {
            var resultAll = true; //think positive;
            var i = 0;
            for (var test in tests) {
                i++;
                //if (i != 16) continue;
                say('' + test + ': ');
                var resultSingle = tryOne(tests[test], false);
                if (resultSingle !== true) {
                    resultAll = false;
                }
            }

            indicate(resultAll);

            return resultAll;
        }
        function sayResult(say) {
            //$('error').innerHTML = $('error').innerHTML + say + '\r\n';
            $('error').value += say + '\r\n';
        }
        function say(say) {
            //$('error').innerHTML = $('error').innerHTML + say + '\r\n';
            $('error').value += say;
        }
    </script>
</head>
<body>
    <img src='logo_r_t_s.png' id='logo' />
    <input type='button' onclick='go()' value='run all tests' />
    <h1>
        agnes Tests</h1>
    <textarea rows='18' cols='96' id='error'>
</textarea>
    <input type='button' id='testButton' onclick='go1($("testNumber").value)' value='run test #:' />
    <input type='text' id='testNumber' />
	<hr />
	<a href='readme.html'>API readme</a>

</body>
</html>
